package main

import (
	"fmt"
	"github.com/go-resty/resty/v2"
	"io/ioutil"
	"log"
	"strconv"
	gconf2 "yundian/cmd/tools/grawl/gconf"
	"yundian/internel/pkg/dbs"
	"yundian/internel/pkg/dingo"
)

func main()  {
	dingo.InitViper("./configs/admin.yaml")
	dbs.NewMysqlComponent().Step()

	url := "http://mng.yundian.ink/mng/10041/user/agent/settlement/orderItem/list.data"

	//doRequestDiviends(19, 3000, "100000", url, "./cmd/grawl/data/dividends/100000/20210102")
	doRequestDiviends(2, 3000, "100067", url, "./cmd/grawl/data/dividends/100067/20210102")
}

func doRequestDiviends(pages, pageSize int, userId, url, path string)  {
	client := resty.New()
	for i := 1; i <= pages; i++ {
		m := map[string]string{}
		m["page"] = strconv.Itoa(i)
		m["limit"] = strconv.Itoa(pageSize)
		m["userId"] = userId  // 用户ID
		resp, err := client.R().
			SetHeader("Content-Type", "application/x-www-form-urlencoded").
			SetFormData(m).
			SetHeader("Cookie", gconf2.Cookie).
			//SetResult(placeType).    // or SetResult(AuthSuccess{}).
			Post(url)
		if err != nil {
			log.Println("抓取失败")
		}
		err = ioutil.WriteFile(fmt.Sprintf(path + "/%d.json",i), resp.Body(),666)
		if err != nil {
			log.Println(err.Error())
		}
	}
}
